﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DingTalkAgent.Model;
using System.Reflection;

namespace DingTalkAgent.Web.Areas.mobile.Controllers
{
    public class mobileBaseController : Controller
    {
        /// <summary>
        /// 
        /// </summary>
        protected _view_account _view_account;
        //会话状态获取
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            requestContext.HttpContext.Session["mobile"] = new _view_account()
            {
                depId = 1,
                depName = "玉溪水利局",
                leveCode = "01",
                isOrg = 1,
                name = "李河长",
                position = "副市长",
                roleid = 1,// 4,
                userid = "0711466326115392",
                corpId = "ding4e928dfcc1b3c5e835c2f4657eb6378f",
                corpSecret = "-MfosVsjkwfj-thDZrOev-vfNWVyGTh-mzuqcEJCmuAo-3XkB6Lp3-382wLKvRx6"
            };
            if (requestContext.HttpContext.Session["mobile"] != null)
            {
                _view_account = (_view_account)requestContext.HttpContext.Session["mobile"];

            }
            else
            {
                if (requestContext.HttpContext.Request.IsAjaxRequest())
                {
                    requestContext.HttpContext.Response.Write("CS00");
                    requestContext.HttpContext.Response.End();
                    return;
                }
                else
                {
                   
                    
                    requestContext.HttpContext.Response.Redirect("~/mobile/login/index?backurl=" + requestContext.HttpContext.Server.UrlEncode(Discuz.Common.DNTRequest.GetUrl()));
                    return;
                }
            }
            ViewBag.skinCode = "default";
            
            ViewBag.url= requestContext.HttpContext.Server.UrlEncode(Discuz.Common.DNTRequest.GetUrl());
            base.Initialize(requestContext);
        }
        public T Parse<T>(HttpRequestBase request, string key, T def = default(T))
        {
            T result = def;
            string str = string.Empty;
            try
            {
                str = request[key];
            }
            catch (Exception ex)
            {
                //logger.ErrorFormat("{0}，参数{1}={2}", ex.Message.ToString(), key, str);
            }
            if (string.IsNullOrWhiteSpace(str))
            {
                return result;
            }
            else
            {
                Type type = typeof(T);
                MethodInfo method = type.GetMethod("TryParse", new Type[] { typeof(String), typeof(T).MakeByRefType() });
                switch (type.ToString())
                {
                    case "System.Single":
                    case "System.Double":
                    case "System.Decimal":
                    case "System.Int32":
                    case "System.Int64":
                    case "System.DateTime":
                    case "System.Boolean":
                        object[] param = new object[2];
                        param[0] = str;
                        object item = method.Invoke(str, param);
                        if ((bool)item)
                        {
                            result = (T)param[1];
                        }
                        break;
                    default:
                        method = type.GetMethod("ToString", new Type[] { });
                        result = (T)method.Invoke(str, null);
                        break;
                }

                return result;
            }
        }

        public T Parse<T>(object obj, T def = default(T))
        {
            if (obj == null)
            {
                return def;
            }
            else
            {
                string str = obj.ToString();
                T result = def;
                Type type = typeof(T);
                MethodInfo method = type.GetMethod("TryParse", new Type[] { typeof(String), typeof(T).MakeByRefType() });
                switch (type.ToString())
                {
                    case "System.Single":
                    case "System.Double":
                    case "System.Decimal":
                    case "System.Int32":
                    case "System.Int64":
                    case "System.DateTime":
                    case "System.Boolean":
                        object[] param = new object[2];
                        param[0] = str;
                        object item = method.Invoke(str, param);
                        if ((bool)item)
                        {
                            result = (T)param[1];
                        }
                        break;
                    default:
                        method = type.GetMethod("ToString", new Type[] { });
                        result = (T)method.Invoke(str, null);
                        break;
                }

                return result;
            }
        }
    }
}